package com.jinzhi.jzweb.controller;

import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.EhCacheUtil;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.MessageplatfromDO;
import com.jinzhi.jzweb.pojo.SendVo;
import com.jinzhi.jzweb.pojo.StuSignUpVo;
import com.jinzhi.jzweb.service.MessageplatfromService;
import com.jinzhi.jzweb.utils.EasyPoiUtil;
import com.jinzhi.sms.support.SmsManager;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <pre>
 * 短信发送
 * </pre>
 * <small> 2020-1-16 16:00 | Kl</small>
 */
@Controller
@RequestMapping("/jzweb/send")
public class sendMessageController extends BaseController {
    @Autowired
    private MessageplatfromService messageplatfromService;
    @Autowired
    private SmsManager smsSerevice;

    @GetMapping()
    @RequiresPermissions("jzweb:send:send")
    String message() {
        return "jzweb/sendMessage";
    }

    /**
     * 群发短信信息
     * @return
     */
    @GetMapping("/sends")
    @RequiresPermissions("jzweb:send:sends")
    String messages() {
        return "jzweb/sendMessages";
    }


    @PostMapping("/messageS")
    @Log("获取短信模板信息")
    @ResponseBody
    public Result<List<Map<String, Object>>> messageS() {
        List<Map<String, Object>> messageplatfromDOList = messageplatfromService.selectMapS();
        return success(messageplatfromDOList);
    }

    @PostMapping("/messageSon")
    @Log("获取短信模板参数信息")
    @ResponseBody
    public Result<Map<String, Object>> messageSon(String id) {
        Map<String, Object> messageplatfromDOList = messageplatfromService.selectMapSon(Long.valueOf(id));
        return success(messageplatfromDOList);
    }

    @PostMapping("/send")
    @Log("主动发送验证码")
    @ResponseBody
    public Result send(String users, String id, String messageContent) {
        if (StringUtils.isNotEmpty(users) && StringUtils.isNotEmpty(id) && StringUtils.isNotEmpty(messageContent)) {

            Pattern p = Pattern.compile("^(\\+86)*0*((13[0-9])|(14[0-9])||(15[0-9])|(18[0-9]))\\d{8}$");
            Matcher m = p.matcher(users);
//            //返回Boolean
//            Boolean b = p.matcher(users).matches();
            if(m.matches()){
                MessageplatfromDO messageplatfromDO = messageplatfromService.selectById(Long.valueOf(id));
                if (messageplatfromDO != null) {
                    if (StringUtils.isNotEmpty(messageplatfromDO.getSmsDemo()) && StringUtils.isNotEmpty(messageplatfromDO.getSign())) {
                        String[] keys = messageContent.split(";");
                        Map templateParam = new HashMap();
                        //循环组装参数
                        for (int i = 0; i < keys.length; i++) {
                            String[] keysSon = keys[i].split(":");
                            if (keysSon.length == 2) {
                                templateParam.put(keysSon[0], keysSon[1]);
                            }
                        }
                        smsSerevice.send1(users, messageplatfromDO.getSmsDemo(), messageplatfromDO.getSign(), templateParam);
                        return Result.build(200, "发送成功");
                    } else {
                        return Result.build(406, "模板信息不全，请检查后重试");
                    }
                } else {
                    return Result.build(405, "未查到对应的短信模板信息");
                }
            }else{
                return Result.build(407, "手机号码错误");
            }
        } else {
            return Result.build(500, "参数为空");
        }
    }

    @PostMapping("/sends")
    @Log("主动群发短信通知")
    @ResponseBody
    public Result sends( String id, String messageContent) {

        if (StringUtils.isNotEmpty(id) ) {
//            //返回Boolean
//            Boolean b = p.matcher(users).matches();
            if(1==1){
                MessageplatfromDO messageplatfromDO = messageplatfromService.selectById(Long.valueOf(id));
                if (messageplatfromDO != null) {
                    if (StringUtils.isNotEmpty(messageplatfromDO.getSmsDemo()) && StringUtils.isNotEmpty(messageplatfromDO.getSign())) {
                        String[] keys = messageContent.split(":");
                        Map templateParam = new HashMap();
                        //循环组装参数
//                        for (int i = 0; i < keys.length; i++) {
//                            String[] keysSon = keys[i].split(":");
//                            if (keysSon.length == 2) {
//                                templateParam.put(keysSon[0], keysSon[1]);
//                            }
//                        }
                        if(keys.length>0){
                            templateParam.put(keys[0], keys[1]);
                            String pgones="17373841816,";
                      //SELECT (SELECT mobile from  sys_user WHERE id=user_id) as mobile,user_id from  jz_experts
                            List<Map<String, Object>>  mapList=  messageplatfromService.selectMobileList();
                            int i=0;
                            int j=0;
                            for (Map limo:mapList){
                                j++;
                                Pattern p = Pattern.compile("^(\\+86)*0*((13[0-9])|(14[0-9])||(15[0-9])|(18[0-9]))\\d{8}$");
                                Matcher m = p.matcher(limo.get("mobile").toString());

                                if(i<1000){
                                    if(m.matches()){
                                        pgones=pgones+limo.get("mobile")+",";

                                        i++;
                                    }
                                }else {
                                    if(j==mapList.size()){
                                        pgones=limo.get("mobile").toString();
                                    }else {
                                        pgones=limo.get("mobile")+",";
                                    }

                                    i=0;
                                }
                                if(i>=1000){
                                    smsSerevice.send1(pgones, messageplatfromDO.getSmsDemo(), messageplatfromDO.getSign(), templateParam);
                                }
                            }

                            if(i<1000){
                                pgones=pgones+"17373841816";
                            String   pgones1="17373841816";
                                 smsSerevice.send1(pgones, messageplatfromDO.getSmsDemo(), messageplatfromDO.getSign(), templateParam);
                               //  smsSerevice.send1(pgones1, messageplatfromDO.getSmsDemo(), messageplatfromDO.getSign(), templateParam);

                            }

                            return Result.build(200, "发送成功");
                        }

                    } else {
                        return Result.build(406, "模板信息不全，请检查后重试");
                    }
                } else {
                    return Result.build(405, "未查到对应的短信模板信息");
                }
            }
//            else{
//                return Result.build(407, "手机号码错误");
//            }
        } else {
            return Result.build(500, "参数为空");
        }
        return Result.build(200, "发送成功");
    }
    @PostMapping("/sendsExl")
    @Log("获取文本内容发送短信主动群发短信通知")
    @ResponseBody
    public Result sendsExl( String id, String messageContent, @RequestParam("file") MultipartFile file) {
        //第一步--判断文件格式
        if (!"application/vnd.ms-excel".equals(file.getContentType())) {
            return Result.fail("文件格式错误，请选择.xls文件");
        }
        //第二步--解析文件获得数据
        List<SendVo> list = EasyPoiUtil.importExcel(file, 1, 1, SendVo.class);
        if (list.size() == 0) {
            return Result.fail("Excel数据为空，请完善");
        }
        Map templateParam = new HashMap();
        int startRow = 0;
//        for (int i = 0; i < list.size(); i++) {
//            StringBuilder sb = new StringBuilder();
//            if (org.apache.commons.lang.StringUtils.isBlank(list.get(i).getName())) {
//                templateParam.put("name",list.get(i).getName().substring(0,1) );
//                sb.append("第" + (i + startRow) + "行姓名未填写，请完善！" + ";");
//                return Result.build(500, "第" + (i + startRow) + "行姓名未填写，请完善！" + ";");
//            }else if(org.apache.commons.lang.StringUtils.isBlank(list.get(i).getMobile())){
//                return Result.build(500, "第" + (i + startRow) + "行电话未填写，请完善！" + ";");
//            }
//        }


        if (StringUtils.isNotEmpty(id) ) {
//            //返回Boolean
//            Boolean b = p.matcher(users).matches();
            if(1==1){
                MessageplatfromDO messageplatfromDO = messageplatfromService.selectById(Long.valueOf(id));
                if (messageplatfromDO != null) {
                    if (StringUtils.isNotEmpty(messageplatfromDO.getSmsDemo()) && StringUtils.isNotEmpty(messageplatfromDO.getSign())) {
                        String[] keys = messageContent.split(":");

                        //循环组装参数
//                        for (int i = 0; i < keys.length; i++) {
//                            String[] keysSon = keys[i].split(":");
//                            if (keysSon.length == 2) {
//                                templateParam.put(keysSon[0], keysSon[1]);
//                            }
//                        }
                        if(keys.length>0){
                            templateParam.put(keys[0], keys[1]);
                            String pgones="";
                            //SELECT (SELECT mobile from  sys_user WHERE id=user_id) as mobile,user_id from  jz_experts
                            //List<Map<String, Object>>  mapList=  messageplatfromService.selectMobileList();
                            int i=0;
                            int j=0;
                            for (SendVo limo:list){
                                j++;
                                Pattern p = Pattern.compile("^(\\+86)*0*((13[0-9])|(14[0-9])||(15[0-9])|(18[0-9]))\\d{8}$");
                                Matcher m = p.matcher(limo.getMobile());

                                if(i<1000){
                                    if(m.matches()){
                                        if(i==list.size()){
                                            pgones=pgones+limo.getMobile();
                                            i++;
                                        }else {
                                            pgones=pgones+limo.getMobile()+",";
                                            i++;
                                        }

                                    }
                                }else {
                                    if(j==list.size()){
                                        pgones=limo.getMobile();
                                    }else {
                                        pgones=limo.getMobile();
                                    }

                                    i=0;
                                }
                                if(i>=1000){
                                    smsSerevice.send1(pgones, messageplatfromDO.getSmsDemo(), messageplatfromDO.getSign(), templateParam);
                                }
                            }

                            if(i<1000){
                                pgones=pgones+"17373841816";
                                String   pgones1="17373841816";
                                   smsSerevice.send1(pgones, messageplatfromDO.getSmsDemo(), messageplatfromDO.getSign(), templateParam);
                                // smsSerevice.send1(pgones1, messageplatfromDO.getSmsDemo(), messageplatfromDO.getSign(), templateParam);

                            }

                            return Result.build(200, "发送成功");
                        }

                    } else {
                        return Result.build(406, "模板信息不全，请检查后重试");
                    }
                } else {
                    return Result.build(405, "未查到对应的短信模板信息");
                }
            }
//            else{
//                return Result.build(407, "手机号码错误");
//            }
        } else {
            return Result.build(500, "参数为空");
        }
        return Result.build(200, "发送成功");
    }
}
